package cn.zhikaizhang.indexviewdemo.demo2;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import cn.zhikaizhang.indexview.PinYinUtil;
import cn.zhikaizhang.indexviewdemo.R;

public class ContactAdapter extends BaseListAdapter<ContactInfo> {

  public ContactAdapter(Context context) {
    super(context);
  }

  @Override public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder = null;
    ContactInfo ContactInfo = getItem(position);
    if (convertView == null) {
      viewHolder = new ViewHolder();
      convertView = LayoutInflater.from(context).inflate(R.layout.list_item2, null);
      viewHolder.indexTextView = (TextView) convertView.findViewById(R.id.index);
      viewHolder.userNameTextView = (TextView) convertView.findViewById(R.id.username);
      convertView.setTag(viewHolder);
    } else {
      viewHolder = (ViewHolder) convertView.getTag();
    }
    viewHolder.userNameTextView.setText(ContactInfo.name);
    char index = PinYinUtil.getIndex(ContactInfo.name);
    if (position == 0 || PinYinUtil.getIndex(getItem(position - 1).name) != index) {
      viewHolder.indexTextView.setVisibility(View.VISIBLE);
      viewHolder.indexTextView.setText(String.valueOf(index));
    } else {
      viewHolder.indexTextView.setVisibility(View.GONE);
    }
    return convertView;
  }

  class ViewHolder {
    TextView indexTextView;
    TextView userNameTextView;
  }
}
